package com.luocj.elephant.demo.view.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ProgressBar;

import com.luocj.elephant.demo.R;

public class HorProgressView1 extends ProgressBar {

    private static final String TAG = HorProgressView1.class.getSimpleName();
    private int defaultColor = Color.parseColor("#F2F2F2");
    private int inColor = Color.parseColor("#3897FC");
    private int outColor = Color.parseColor("#9BCAFB");
//    private int inColor = Color.parseColor("#ff0000");
//    private int outColor = Color.parseColor("#00ff00");

    private int width;
    private int height;
    private int result;
    private Paint defaultPaint;
    private Paint reachPaint;

    //剩余人数
    private int currentLeftNum;
    //当月入职人数
    private int currentInNum = 0;
    //当月离职人数
    private int currentOutNum = 0;


    public HorProgressView1(Context context) {
        this(context, null);
    }

    public HorProgressView1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("CustomViewStyleable")
    public HorProgressView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Hor_Progress_View_1);
//        width = (int) ta.getDimension(R.styleable.Hor_Progress_View_1_width_1, width);
//        height = (int) ta.getDimension(R.styleable.Hor_Progress_View_1_height_1, height);
        defaultColor = ta.getColor(R.styleable.Hor_Progress_View_1_defaultColor_1, defaultColor);
        outColor = ta.getColor(R.styleable.Hor_Progress_View_1_defaultColor_1, outColor);

        ta.recycle();

    }

    private void initPaint() {
        defaultPaint = new Paint();
        defaultPaint.setColor(defaultColor);
        defaultPaint.setStrokeWidth(height);
        defaultPaint.setStyle(Paint.Style.FILL);
        defaultPaint.setAntiAlias(true);
//        defaultPaint.setPathEffect(new CornerPathEffect(10));

        reachPaint = new Paint();
        reachPaint.setColor(outColor);
//        reachPaint.setPathEffect(new CornerPathEffect(10));
//        reachPaint.setStrokeCap(Paint.Cap.ROUND);
        reachPaint.setAntiAlias(true);
        reachPaint.setStrokeWidth(height);
        reachPaint.setStyle(Paint.Style.FILL);
        Log.i(TAG, "initPaint: " + width + "height:->" + height);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);


        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = result;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = result;
        }


        setMeasuredDimension(width, height);

        Log.i(TAG, "onMeasure: " + width + " height = " + height);

    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        initPaint();

        canvas.drawColor(defaultColor);
//        canvas.translate(0, getHeight() / 2);

        if (currentInNum == 0 && currentOutNum == 0) return;
        defaultPaint.setColor(inColor);
        canvas.drawLine(0, getHeight() / 2, (float) (0.3 * width), getHeight() / 2, defaultPaint);

//        defaultPaint.setColor(outColor);
//        canvas.drawLine((float) (0.3 * width), getHeight() / 2, (float) (0.5 * width), getHeight() / 2, defaultPaint);
    }


    public int getCurrentLeftNum() {
        return currentLeftNum;
    }

    public void setCurrentLeftNum(int currentLeftNum) {
        this.currentLeftNum = currentLeftNum;
    }

    public int getCurrentInNum() {
        return currentInNum;
    }

    public void setCurrentInNum(int currentInNum) {
        this.currentInNum = currentInNum;
    }

    public int getCurrentOutNum() {
        return currentOutNum;
    }

    public void setCurrentOutNum(int currentOutNum) {
        this.currentOutNum = currentOutNum;
    }

}
